package com.ai.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.WxKeywordConfigService;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
import com.ai.web.vo.WxKeywordConfigV;
/**
 * 
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月28日
*
 */
@Controller
@RequestMapping("/wxKeywordConfig")
public class WxKeywordConfigController extends BaseController {
    
    
    private static Logger logger = Logger.getLogger(WxKeywordConfigController.class);
    
    @Autowired
    private WxKeywordConfigService wxKeywordConfigService;
    
    @RequestMapping(value = "/list")
    public String list(@RequestParam(defaultValue = "") String username,
            @RequestParam(defaultValue = "") String password) {
        
        logger.info("hello......test");
        return "wxKeywordConfig/wxKeywordConfig_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        return wxKeywordConfigService.queryListAndCount(conditionMap);
    }
    
    @RequestMapping(value = "/new")
    public String newwxKeywordConfig(Model model) {
        
        WxKeywordConfigV wxKeywordConfigV = new WxKeywordConfigV();
        model.addAttribute("wxKeywordConfigV", wxKeywordConfigV);
        
        return "wxKeywordConfig/wxKeywordConfig_template";
    }
    
    @RequestMapping(value = "/update/{id}")
    public String updatewxKeywordConfig(@PathVariable("id") int userId, Model model)
            throws Exception {
        
        WxKeywordConfigV wxKeywordConfigV = wxKeywordConfigService.getWxKeywordConfig(userId);
        logger.info("----- getUser:" + wxKeywordConfigV);
        
        if (wxKeywordConfigV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute("wxKeywordConfigV", wxKeywordConfigV);
        
        return "wxKeywordConfig/wxKeywordConfig_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel savewxKeywordConfig(WxKeywordConfigV wxKeywordConfigV) throws Exception {
        
        logger.info("-----save:" + wxKeywordConfigV);
        ResultModel rm = wxKeywordConfigService.saveWxKeywordConfig(wxKeywordConfigV);
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deletewxKeywordConfig(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = wxKeywordConfigService.deleteWxKeywordConfigs(userIds);
        
        return rm;
    }
    
}
